
function validateUsername(fld) {
	// alert("validateUsername()");
	var error = "";

	if (fld.value == "") {
		error = "* You did not enter User Name \n";
		fld.style.background = 'Yellow';
	} else if (fld.value.length < 5 || fld.value.length > 15) {
		error = "* User Name should be beteen 5 to 15 characters long\n";
		fld.style.background = 'Yellow';
	} else {
		fld.style.background = 'White';
	}
	return error;
}

function validatePassword(fld) {
	var error = "";

	if (fld.value == "") {
		error = "* You did not enter Password\n";
		fld.style.background = 'Yellow';
	} else if (fld.value.length < 5 || fld.value.length > 15) {
		error = "* Incorrect length of Password. Password should be between 5 to 15 characters long\n";
		fld.style.background = 'Yellow';
	} else {
		fld.style.background = 'White';
	}
	return error;
}

function trim(s) {
	return s.replace(/^\s+|\s+$/, '');
}

function validateEmail(fld) {
	var error = "";
	var tfld = trim(fld.value); // value of field with whitespace trimmed off
	var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/;
	var illegalChars = /[\(\)\<\>\,\;\:\\\"\[\]]/;

	if (fld.value == "") {
		fld.style.background = 'Yellow';
		error = "* You didn't enter Email address.\n";
	} else if (!emailFilter.test(tfld)) { // test email for illegal characters
		fld.style.background = 'Yellow';
		error = "* Please enter a valid email address.(Eg. xyz@gmail.com)\n";
	} else if (fld.value.match(illegalChars)) {
		fld.style.background = 'Yellow';
		error = "* The Email address contains illegal characters.\n";
	} else {
		fld.style.background = 'White';
	}
	return error;
}

function validateCompanyName(fld) {
	var error = "";

	if (fld.value == "") {
		fld.style.background = 'Yellow';
		error = "* You did not enter Company Name\n";
	} else if (fld.value.length < 2) {
		fld.style.background = 'Yellow';
		error = "* Company Name is too small\n";
	} else {
		fld.style.background = 'White';
	}
	return error;
}

function validateCompanyProfile(fld) {
	var error = "";

	if (fld.value == "") {
		fld.style.background = 'Yellow';
		error = "* You didn't enter Company Profile\n";
	} else if (fld.value.length < 10) {
		fld.style.background = 'Yellow';
		error = "* Company Profile is too small, Please make it more descriptive. It can be upto 300 characters long\n";
	} else {
		fld.style.background = 'White';
	}
	return error;
}

function validateContactPerson(fld) {
	var error = "";

	if (fld.value == "") {
		fld.style.background = 'Yellow';
		error = "* You didn't enter Contact Person name\n";
	} else {
		fld.style.background = 'white';
	}
	return error;
}
function validateAddress(fld) {
	// alert("validateAddress()");
	var error = "";

	if (fld.value == "") {
		fld.style.background = 'Yellow';
		error = "* You didn't enter Address\n";
	} else if (fld.value.length < 10) {
		fld.style.background = 'Yellow';
		error = "* Address is too small, Please enter detailed address with street name\n";
	} else {
		fld.style.background = 'White';
	}
	return error;
}

function validatePhone(fld) {
	// alert("validatePhone()");
	var error = "";
	var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');

	if (fld.value == "") {
		error = "* You didn't enter a phone number.\n";
		fld.style.background = 'Yellow';
	} else if (isNaN(parseInt(stripped))) {
		error = "* The phone number contains illegal characters.\n";
		fld.style.background = 'Yellow';
	} else if (!(stripped.length == 10)) {
		error = "* The phone number is the wrong length. It should be 10 digit number(Without country code.)\n";
		fld.style.background = 'Yellow';
	} else {
		fld.style.background = 'White';
	}
	return error;
}

function validatePincode(fld) {
	// alert("validatePincode()");
	var error = "";
	var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');

	if (fld.value == "") {
		error = "* You didn't enter a Pin Code.\n";
		fld.style.background = 'Yellow';
	} else if (isNaN(parseInt(stripped))) {
		error = "* The Pin Code contains illegal characters.\n";
		fld.style.background = 'Yellow';
	} else if (!(stripped.length == 6)) {
		error = "* The Pin Code is of wrong length. It should be 6 digit number\n";
		fld.style.background = 'Yellow';
	} else {
		fld.style.background = 'White';
	}
	return error;
}

function validateFormOnSubmit(theForm) {
	var reason = "";
	// alert("validateFormOnSubmit()");

	reason += validateUsername(theForm.username);
	reason += validatePassword(theForm.password);
	reason += validateEmail(theForm.email);
	reason += validateCompanyName(theForm.companyName);
	reason += validateCompanyProfile(theForm.companyProfile);
	reason += validateContactPerson(theForm.contactPerson);
	reason += validateAddress(theForm.address);
	reason += validatePincode(theForm.pincode);
	reason += validatePhone(theForm.phone);

	if (reason != "") {
		alert("Some fields need correction:\n \n" + reason);
		return false;
	}
	// alert("all validations successful returning true");
	return true;
}